﻿using DevExpress.XtraEditors;
using JXC.Core;
using JXC.Models;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using JXC.WebApi.Entity;

namespace JXC.UI.Common
{
    public partial class SPXXImportDataForm : DevExpress.XtraEditors.XtraForm
    {
        private APIClient client = new APIClient();

        public SPXXImportDataForm()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Files|*.xls;*.xlsx";
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string filePath = "";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // 取得文件路径及文件名
                filePath = openFileDialog.FileName;
                gridControl1.DataSource = null;
                var dataTable = ExtensionMethod.ExcelToDatatable(filePath, null, true);
                List<spxx> list = new List<spxx>();
                foreach (DataRow dataTableRow in dataTable.Rows)
                {
                    spxx spxx = new spxx();
                    //spxx.SPCode = "SP" + (client.GetLastSPCode().TrimStart("SP".ToArray()).ObjToInt() + 1).ToString("00000");
                    spxx.SPName = dataTableRow[0].ToString();
                    spxx.GGXH = dataTableRow[1].ToString();
                    spxx.SPLBName = dataTableRow[2].ToString();
                    spxx.YSSJ = dataTableRow[3].ObjToDecimal();
                    spxx.DW = dataTableRow[4].ToString();
                    spxx.YSJJ = dataTableRow[5].ObjToDecimal();
                    spxx.QCKC = dataTableRow[6].ObjToDecimal();
                    spxx.KCXX = dataTableRow[7].ObjToDecimal();
                    list.Add(spxx);
                }
                gridControl1.DataSource = list;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo(Application.StartupPath + "/ImportExecl/商品导入模板.xlsx");
            Process process = new Process();
            process.StartInfo = processStartInfo;
            process.StartInfo.UseShellExecute = true;
            process.Start();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if(gridView1.RowCount==0)return;
            var list=(List<spxx>)gridView1.DataSource;
            var num = client.GetLastSPCode().TrimStart("SP".ToArray()).ObjToInt();
            list.ForEach(p =>
            {
                num++;
                p.SPCode= "SP" + (num).ToString("00000");
                if (!client.GetDicDataList().Where(n => n.DicTypeName.Equals("商品类别"))
                    .Any(n => n.DicData.Equals(p.SPLBName)))
                {
                    client.AddDicData(new dicData(){DicData = p.SPLBName,DicTypeName = "商品类别" });
                }
                if (!client.GetDicDataList().Where(n => n.DicTypeName.Equals("商品单位"))
                        .Any(n => n.DicData.Equals(p.DW)))
                {
                    client.AddDicData(new dicData() { DicData = p.DW, DicTypeName = "商品单位" });
                }
            });
            if (client.AddSPList(list))
            {
                "导入成功".ShowTips();
                DialogResult= DialogResult.OK;
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}